package com.dimeng.utils;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;

/**
 * @author write by ccn<br>
 * <b>helloandroid100@gmail.com</b>
 * @version 0.1;2012-5-18 下午5:42:38<br>
 * descrition:异步加载图片
 * 		http://blog.orange11.nl/2009/09/17/exploring-the-world-of-android-part-2/
 *      http://www.iteye.com/topic/685986
 *      http://hulefei29.iteye.com/blog/697455
 */
public class AsyncImageLoader {
	/**软引用缓存**/
	private static HashMap<String, SoftReference<Drawable>> imageCache = new HashMap<String, SoftReference<Drawable>>();
	//在线程池里生成5个线程
	private  ExecutorService service = Executors.newFixedThreadPool(10);
	private static AsyncImageLoader instance = new AsyncImageLoader(); 
    private AsyncImageLoader() {}
    public static AsyncImageLoader getInstance(){
    	return instance;
    }
 
    public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {
        Drawable drawable = getDrawableFromSoftRef(imageUrl);
        if (drawable != null) {
            return drawable;
        }
        
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message message) {
                imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
            }
        };
        service.submit(new Runnable() {
             public void run() {
             	int i=0;
             	while(++i<3){
             		Bitmap bitmap = CommonUtil.loadBitmap(imageUrl);
             		if(null != bitmap){
             			Drawable drawable = new BitmapDrawable(bitmap);
             			if(!getImageCache().containsKey(imageUrl))
             				getImageCache().put(imageUrl, new SoftReference<Drawable>(drawable));
             			Message message = handler.obtainMessage(0, drawable);
             			handler.sendMessage(message);
             			break;
             		}
             		try {
 						Thread.sleep(500);
 					} catch (InterruptedException e) {
 						e.printStackTrace();
 					}
             	}
             }
		});
        
        return null;
    }
    
    public interface ImageCallback {
        public void imageLoaded(Drawable imageDrawable, String imageUrl);
    }
    /**软引用缓存**/
	public static synchronized HashMap<String, SoftReference<Drawable>> getImageCache() {
		return imageCache;
	}
	
	public static synchronized Drawable getDrawableFromSoftRef(String imageUrl){
		if (getImageCache().containsKey(imageUrl)) {
            SoftReference<Drawable> softReference = getImageCache().get(imageUrl);
            Drawable drawable = softReference.get();
            if (drawable != null) {
//            	System.out.println("get bitmap from softReference memory url="+imageUrl);
                return drawable;
            }
        }
		return null;
	}
}
